<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class founder_tasks extends CI_Controller {

 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('hrm/founder/task_model');	
		//$this->load->model('user/task_model');	
	}
	
	function prabhas(){
		echo "i am prabhas";exit;
	}

function index()
   {
   	//echo "i am here";exit;
	  $this->data['tasks'] = $this->task_model->get_all_tasks(); 
	  $meta['page_title'] = "All Task";
	  $meta['dashboard'] = "dashboard";	  
	  $this->load->view('common/header', $meta);
	  $this->load->view('hrm/founder/tasks',$this->data);
      $this->load->view('common/footer');
   }
   
  function create_tasks($edit_id = NULL)
   {
 //  	echo "i am here";exit;
	   if ($this->ion_auth->in_group('manager')){
	   $this->form_validation->set_rules('user', 'User', 'required');
	   }
	   
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('desc', 'Description', 'required');
		$this->form_validation->set_rules('start_date', 'Start Date', 'required');
		$this->form_validation->set_rules('end_date', 'End Date', 'required');
		$this->form_validation->set_rules('priority', 'Priority', 'required');
		
		//$edit_id = null;
		$edit_id = $this->input->post('edit_id');
		if(!empty($edit_id)) {			
			$this->form_validation->set_rules('edit_id', 'Id', 'required');
			}
		
		if ($this->form_validation->run() == FALSE)
		{
			
			//$this->form_validation->set_message('user');
			$this->form_validation->set_message('title');
			$this->form_validation->set_message('desc');
			$this->form_validation->set_message('start_date');
			$this->form_validation->set_message('end_date');		
		}
		else
		{
						
			$for = ($this->input->post('user')) ? $this->input->post('user') : $this->session->userdata('user_id');			
			
			$edit_id = $this->input->post('edit_id');
			if(!empty($edit_id))
			{
			$update_insert = array(
			'user_id' => $for,
			'title' => $this->input->post('title'),
			'desc' => $this->input->post('desc'),
			'start_date' => $this->input->post('start_date'),
			'end_date' => $this->input->post('end_date'),
			'priority' => $this->input->post('priority'),
			);
			
			$this->task_model->update($update_insert,$edit_id);
				redirect('hrm/founder_tasks');
			}
			else {

			$data = array(
			'user_id' => $for,
			'title' => $this->input->post('title'),
			'desc' => $this->input->post('desc'),
			'start_date' => $this->input->post('start_date'),
			'end_date' => $this->input->post('end_date'),
			'priority' => $this->input->post('priority'),
			);			
			
			$insert_id = $this->task_model->create_task($data);
			
			
			$subtasks = $this->input->post('subtask');		
			if(isset($subtasks))				{
			
			foreach($subtasks as $subtask)			
					{
				$insert_subtasks = array(
				'task_id' => $insert_id,
				'user_id' => $for,
				'desc'=> $subtask,
				);				
		$this->task_model->create_subtask($insert_subtasks);
					}
				}
			}
			
			redirect('hrm/founder_tasks/milestones_details/'.$this->input->post('project'));		
			//$this->load->view('formsuccess');
			
		}
	
		$this->data['users'] = $this->ion_auth->users()->result();	 
		
		
	     $meta['page_title'] = "Create a New Task";
		 
		 $this->data['projects'] = $this->task_model->get_all_projects();
		$this->data['milestones'] = $this->task_model->get_default_milestone();
		$this->data['taskgroups'] = $this->task_model->get_default_taskgroups();
		 $meta['dashboard'] = "dashboard";	   
	  	 
		 $this->load->view('common/header', $meta);
	   	 $this->load->view('hrm/founder_create-task',$this->data);
         $this->load->view('common/footer');
   }
  
   function edit($edit_id)
   {
		$this->data['users'] = $this->ion_auth->users()->result();	 
		$this->data['edit'] = $data = $this->task_model->edit($edit_id);
		
		//print_r($this->data['users']);exit;
		 $meta['page_title'] = "Edit Task";	  
	    $meta['dashboard'] = "dashboard";	   
	   $this->load->view('common/header', $meta);
	   $this->load->view('hrm/founder_create-task',$this->data);
       $this->load->view('common/footer');
   }
  
  function task_detail($edit_id = null)
  {
	 $edit_id = isset($edit_id) ? $edit_id : $this->session->flashdata('edit_id');
	
	$meta['page_title'] = "Create Task";
	
	$data['tasks'] = $this->task_model->get_task($edit_id);
	$data['notes'] = $this->task_model->get_note_by_id($edit_id);
	$data['task_id'] = $this->uri->segment(4);
	$data['subtasks'] = $this->task_model->get_subtask($edit_id);
		 $meta['dashboard'] = "dashboard";	   
	  	
	  $this->load->view('common/header', $meta);
	  $this->load->view('hrm/founder/tasks-detail',$data);
      $this->load->view('common/footer');
  }
  
  
  function add_note()
   {
	  $insert=array(
		   'user_id' => $this->session->userdata('user_id'), 
		   'task_id' =>	$_REQUEST['taskid'],
		   'note' 	 =>	$_REQUEST['note'],
		   'date'	 => date('Y-m-d')
	  	);		
	  $add = $this->task_model->add_note($insert);
	}
	
	function delete_note($note_id,$task_id)
		{
			
			$this->task_model->delete_note($note_id);
			$this->session->set_flashdata('note_message', 'Note delted successfully');
			
			redirect('hrm/user/tasks/task_detail/'.$task_id,'refresh');
		}
	
	function task_achieved()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_achieved' => 1
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->is_achieved($update,$check);			
		}
		
			function task_unachieved()
		{
			$update = array(			
			'is_achieved' => 0
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			//print_r($check);
			//exit;
			$this->task_model->task_unachieved($update,$check);
		}
		
		function task_closed()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_completed' => 1
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->task_closed($update,$check);
			
		}
		
		function task_open()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_completed' => 0
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->task_open($update,$check);
			
		}
		
		function check_is_achieved()
		{
			$task_id = $_REQUEST['taskid'];
			$value = $this->task_model->check_is_achieved($task_id);
			echo $value['is_achieved'];	
			
		}
		
		function check_is_completed()
		{
		$task_id = $_REQUEST['taskid'];
		$value = $this->task_model->check_is_completed($task_id);
			echo $value['is_completed'];	
		}
		
		// sub tasks functions 		
		function create_subtask($task_id)
		{
			$insert_subtasks = array(
				'task_id' => $task_id,
				'user_id' => $this->session->userdata('user_id'),
				'desc'=> $this->input->post('subtask'),
				'is_achieved'=>0
				);
				$value = $this->task_model->create_subtask($insert_subtasks);
			//	$this->load->view('common/header');
			redirect('hrm/founder_tasks/task_detail/'.$task_id);
			//$this->form_validation->set_rules('subtask', 'Subtask', 'required');
		}
		
		function subtask_achieved()
		{
			 $subtask_id = $_REQUEST['subtask_id'];
			 $value = $this->task_model->subtask_achieved($subtask_id);
		}
		
		function subtask_unachieved()
		{
			 $subtask_id = $_REQUEST['subtask_id'];
			 $value = $this->task_model->subtask_unachieved($subtask_id);
		}
		
		function check_subtask_is_achieved()
		{
			$subtask_id = $_REQUEST['subtask_id'];
			$value = $this->task_model->check_subtask_is_achieved($subtask_id);
			echo $value['is_achieved'];
		}
		
		function delete_subtask($task_id,$delete_id)
		{
		 $this->task_model->delete_subtask($delete_id);	
		//$this->db->delete('mytable', array('id' => $delete_id)); 
		redirect('hrm/founder_tasks/task_detail/'.$task_id,'refresh');
		}
	
}
	
	?>